import * as repairService from '../../services/repair';
import { queryAssetType } from '../../services/assetType';
import { qryAllUser } from '../../services/user';
import { queryAssetBrand, queryAssetModelAll } from '../../services/assetModel';
import { queryRoomList } from '../../services/tRoom';
import { querAssetListByParam } from '../../services/equipment';

export default {
  namespace: 'repair',
  state: {
    list: [],
    assetTypeTree: [],
    deptTree: [],
    roomList: [],
    assetBrandList: [],    
    assetModelList:[],
    userList: [],
    assetList: [],
    current: 1,
    currentSize: 10,
    total: 0,
    modalVisible: false,
    modalType: 'create',
    newKey: '',
    detailList: [],
    currentItem: {},
    viewModalVisible: false,
    viewItem: {},
    viewDetailList: [],
    pullOffId: "",
    detailModalVisible: false,
    secondModalVisible: false,
    assetItem: {},
    assetCurrent: 1,
    assetCurrentSize: 10,
    assetTotal: 0,
    repairRemarkViewItem: {},
    qryAssetItem:{},
    doRepairViewItem:{},
  },
  reducers: {
    updateState (state, { payload }) {
      return { ...state, ...payload}
    },
    queryListSuc(state, { payload }) {
      return { ...state, ...payload };
    },
    queryRoomListSuc(state, { payload: roomList }) {
      return { ...state, roomList };
    },
    queryListByParamSuc(state, { payload }) {
      return { ...state, ...payload };
    },
    changeDetailList(state, { payload }) {
      return { ...state, ...payload, secondModalVisible: false };
    },
    queryUserSuc(state, { payload: userList }) {
      return { ...state, userList };
    },
    showViewModal(state, { payload }) {
      return { ...state, ...payload, newKey: new Date().getTime() + '', viewModalVisible: true }
    },
    hideViewModal(state) {
      return { ...state, viewModalVisible: false, newKey: new Date().getTime() + '' }
    },
    queryAssetTypesSuc(state, { payload: assetTypeTree }) {
      return { ...state, assetTypeTree };
    },
    queryBrandListSuc(state, { payload: assetBrandList }) {
      return { ...state, assetBrandList };
    },
    *queryAssetModel({ payload },{ call, put }) {
      const { body } = yield call(queryAssetModelAll,payload);
      yield put({ type: 'updateState', payload: {assetModelList:body.data} });
    },
    showAddDetailModal(state, { payload }) {
      return { ...state, ...payload, addDetailModalVisible: true }
    },
    hideAddDetailModal(state) {
      return { ...state, addDetailModalVisible: false }
    },
    getJoberNumSuc(state, { payload }) {
      return { ...state, pullOffId: payload };
    },
    showModal(state, { payload }) {
      return { ...state, ...payload, newKey: new Date().getTime() + '', modalVisible: true }
    },
    hideModal(state,{ payload }) {
      return { ...state,...payload, modalVisible: false, detailList: [], newKey: new Date().getTime() + '' }
    },
    showSecondModal(state, { payload }) {
      return { ...state, ...payload, secondModalVisible: true }
    },
    hideSecondModal(state) {
      return { ...state, secondModalVisible: false }
    },
    showDoRepairViewModal(state, { payload }) {
      return { ...state, ...payload, newKey: new Date().getTime() + '', doRepairModalVisible: true }
    },
    hideDoRepairViewModal(state, { payload }) {
      return { ...state, ...payload, newKey: new Date().getTime() + '', doRepairModalVisible: false }
    },

    showRepairRemarkViewModal(state, { payload }) {
      return { ...state, ...payload, repairRemarkModalVisible: true }
    },
    hideRepairRemarkViewModal(state) {
      return { ...state, repairRemarkModalVisible: false }
    },
  },
  effects: {
    *queryUser({ payload }, { call, put }) {
      const { body } = yield call(qryAllUser);
      yield put({ type: 'queryUserSuc', payload: body.data });
    },
    *queryRoom({ payload }, { call, put }) {
      const { body } = yield call(queryRoomList, payload);
      yield put({ type: 'queryRoomListSuc', payload: body.data });
    },
    *queryAssetType({ payload }, { call, put }) {
      const { body } = yield call(queryAssetType);
      yield put({ type: 'queryAssetTypesSuc', payload: body.data });
    },
    *queryAssetBrand({ payload }, { call, put }) {
      const { body } = yield call(queryAssetBrand, payload);
      yield put({ type: 'queryBrandListSuc', payload: body.data });
    },
    *querAssetListByParam({ payload }, { call, put }) {
      const { body } = yield call(repairService.querAssetListByParam, payload);
      yield put({ type: 'queryListByParamSuc', payload: { assetList: body.data, assetCurrent: body.current, assetCurrentSize: body.size, assetTotal: body.iTotalRecords } });
    },
    *qryAssetById({ payload }, { call, put }) {
      // const { body } = yield call(repairService.qryAssetById, payload);
      yield put({ type: 'showAddDetailModal', payload: { assetItem:payload} });
    },

    *editRepairRemark({ payload }, { call, put }) {
      yield put({ type: 'showRepairRemarkViewModal', payload: { repairRemarkViewItem: payload, } });
    },
    *queryAssetList({ payload }, { call, put }) {
      yield put({ type: 'querAssetListByParam', payload: { roomId: payload.roomId,searchStatusType:4} });
      yield put({ type: 'showSecondModal', payload: { qryAssetItem: payload } });
    },
    *queryList({ payload }, { call, put }) {
      const { body } = yield call(repairService.queryList, payload);
      yield put({ type: 'queryListSuc', payload: { list: body.data, current: body.current, currentSize: body.size, total: body.iTotalRecords } });
    },
    *onViewItem({ payload }, { call, put }) {
      const { body } = yield call(repairService.qryById, payload);
      yield put({
        type: 'showViewModal', payload: { viewItem: body.data, viewDetailList: body.data.detailList },
      });
    },
    *onDoRepairViewItem({ payload }, { call, put }) {
      const { body } = yield call(repairService.qryById, payload);
      yield put({
        type: 'showDoRepairViewModal', payload: { doRepairViewItem: body.data, viewDetailList: body.data.detailList },
      });
    },
    *doRepair({ payload }, { call, put }) {
      const { body } = yield call(repairService.doRepair, payload);
      yield put({ type: 'hideDoRepairViewModal' });
      yield put({ type: 'reload' });
    },

    *qryById({ payload }, { call, put }) {
      const { body } = yield call(repairService.getJoberNum, payload);
      yield put({ type: 'getJoberNumSuc', payload: body.data, });
      yield put({ type: 'showModal' });
      yield put({ type: 'queryAssetType' });
      yield put({ type: 'queryAssetBrand', payload: {}, });
    },
    *addRepair({ payload }, { call, put }) {
      const { body } = yield call(repairService.create, payload);
      yield put({ type: 'hideModal' });
      yield put({ type: 'reload' });
    },
    *remove({ payload }, { call, put }) {
      const { body } = yield call(repairService.remove, payload.id);
      yield put({ type: 'queryList', payload: payload.search });
    },
    *reload(action, { put }) {
      yield put({ type: 'queryList', payload: {} });
    },
  },
  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname }) => {
        if (pathname === '/repair') {
          dispatch({ type: 'queryUser' });
          dispatch({ type: 'queryRoom' });
          dispatch({ type: 'queryList', payload: {} });
        }
      });
    },
  },
};
